package com.guagua.chat.post;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.guagua.util.HazelcastCollectionUtil;

/**
 * 北京八度互联科技有限公司版权所有
 * Copyright (C) Badu Corporation. All Rights Reserved
 */
/** ******************************************************************
* 类概要:贴子管理类
* @author 李一
* @version 2012-7-2 9:00:00
*******************************************************************/
public class PostManager {
	

	private static final PostManager _POST_MANAGER = new PostManager();
	private static final String HAZELCASE_MAPNAME ="ROOMPOSTS";//缓存MAPNAME
	private static final int _ROWS = 10;//每页显示几行
	private PostDao postDao = new PostDao();
	
	private PostManager(){}
	
	/**
	 * 单例模式-返回实例
	 * @return PostManager
	 */
	public static PostManager getInstance(){
		return _POST_MANAGER;
	}
	
	
	/**
	 * 新增贴子主题或对主题进行评论
	 * @param PostInfo
	 * @param roomId
	 * @return
	 */
	public boolean createPostOrComment(PostInfo PostInfo,String roomId){
		HazelcastCollectionUtil.destory(this.HAZELCASE_MAPNAME+"_"+roomId);
		if(PostInfo.getParent_id()!=0L){
			HazelcastCollectionUtil.destory(this.HAZELCASE_MAPNAME+"_"+roomId+"_"+PostInfo.getParent_id());
		}
		return postDao.createPostOrComment(PostInfo, roomId);
	}
	/**
	 * 返回贴子分页数据
	 * @param roomId
	 * @param pageNo
	 * @return map list-data:分页数据  count:数量
	 * @throws Exception
	 */
	public Map<String,Object> findPostAllByRoomId(String roomId,int pageNo) throws Exception{
		Map<String,Object> map = null;
		String key ="POSTLIST";
		if(pageNo == 1){//首页从缓存中获取
			Map _temp = HazelcastCollectionUtil.getMap(this.HAZELCASE_MAPNAME+"_"+roomId);
			if(_temp!=null && _temp.get(key)!=null){//取到数据
				map = (Map<String,Object>)_temp.get(key);
			}
		}
		if(map == null){//从数据库中读取
			map = new HashMap();
			List<PostInfo> list = postDao.findPostsByRoomId(roomId, pageNo, _ROWS);//数据
			int count = postDao.getPostCountByRoomId(roomId);
			map.put("listdata", list);
			map.put("count", count);
			//放入缓存
			if(pageNo == 1){
				HazelcastCollectionUtil.putMapWithTime(HAZELCASE_MAPNAME+"_"+roomId, key, map, 30*60L, TimeUnit.SECONDS);
			}
			
		}
		
		return map;
	}
	/**
	 * 删除贴子主题
	 * @param roomId
	 * @param id
	 * @return
	 */
	public boolean delPostById(String roomId,long id){
		//删除主题时清除缓存
		HazelcastCollectionUtil.destory(this.HAZELCASE_MAPNAME+"_"+roomId);
		return postDao.delPostById(id, roomId);
	}
	/**
	 * 返回评论分页数据
	 * @param roomId
	 * @param PostId
	 * @param pageNo
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> findPostCommentsByRoomId(String roomId,long postId,int pageNo)throws Exception{
		//更新访问数
		postDao.updPageView(roomId, postId);
		List< PostInfo> cachePostList = null;
		HazelcastCollectionUtil.destory(this.HAZELCASE_MAPNAME+"_"+roomId);//销毁
		
		
		
		String key ="POSTCOMMENTLIST";
		Map<String,Object> map =null;
		if(pageNo == 1){
			Map _temp = HazelcastCollectionUtil.getMap(this.HAZELCASE_MAPNAME+"_"+roomId+"_"+postId);
			if(_temp!=null && _temp.get(key)!=null){//取到数据
				map = (Map<String,Object>)_temp.get(key);
			}
		}
		if(map == null){//从数据库中读取
			map = new HashMap();
			List<PostInfo> list = postDao.findPostCommentsByRoomId(roomId, postId, pageNo, _ROWS);//数据
			int count = postDao.getCommentCountByPostId(roomId, postId);
			map.put("listdata", list);
			map.put("count", count);
			map.put("post", postDao.getPostInfoById(roomId, postId)); 
			//放入缓存
			if(pageNo == 1){
				HazelcastCollectionUtil.putMapWithTime(this.HAZELCASE_MAPNAME+"_"+roomId+"_"+postId, key, map, 10*60L, TimeUnit.SECONDS);
			}
		}
		
		return map;
	}
	/**
	 * 删除评论
	 * @param id
	 * @param parentId
	 * @param roomId
	 * @return
	 */
	public boolean delCommentById(long id,long parentId,String roomId){
		HazelcastCollectionUtil.destory(this.HAZELCASE_MAPNAME+"_"+roomId);
		HazelcastCollectionUtil.destory(this.HAZELCASE_MAPNAME+"_"+roomId+"_"+parentId);
		return postDao.delCommentById(id, parentId,roomId);
	}
	
	
	
}
